package com.wonder.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.admin.service.IVtuzxKingdeeService;
import com.wonder.app.domain.*;
import com.wonder.app.mapper.*;
import com.wonder.app.request.QingPurInRequest;
import com.wonder.app.service.*;
import com.wonder.bean.kingdee.VtuzxKingdeeCloudClient;
import com.wonder.bean.kingdee.xqc.VtuzxArrayList;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.utils.DateUtils;
import com.wonder.common.utils.MessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class QingPurInServiceImpl extends ServiceImpl<QingPurInDao, QingPurIn> implements QingPurInService {
    /** 日志 */
    private static final Logger _logger = LoggerFactory.getLogger(QingPurInServiceImpl.class);


    @Autowired
    private MaterialService materialService;


    @Autowired
    private QingPurInDao qingPurInDao;

    @Autowired
    private QingPurInDetailDao qingPurInDetailDao;

    @Autowired
    private IVtuzxKingdeeService vtuzxKingdeeService;

    @Autowired
    private QingPurInService qingPurInService;

    @Autowired
    private  QingPurInDetailService qingPurInDetailService;

    @Autowired
    private SyncKingdeeOrderService syncKingdeeOrderService;
    @Autowired
    private QingSyncOrderService qingSyncOrderService;

    @Autowired
    private QingSyncOrderDetailService qingSyncOrderDetailService;

    @Autowired
    private QingSyncOrderDetailDao qingSyncOrderDetailDao;



    @Override
    public IPage<QingPurIn> selectList(QingPurInRequest qingPurInRequest, Page<QingPurIn> page) {
        LambdaQueryWrapper<QingPurIn> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (!VtuzxUtil.isEmpty(qingPurInRequest.getSyncNo())){
            lambdaQueryWrapper.like(QingPurIn::getSyncNo,qingPurInRequest.getSyncNo());
        }
        if (!VtuzxUtil.isEmpty(qingPurInRequest.getPurBillNo())){
            lambdaQueryWrapper.like(QingPurIn::getPurBillNo,qingPurInRequest.getPurBillNo());
        }
        if (!VtuzxUtil.isEmpty(qingPurInRequest.getRkBillNo())){
            lambdaQueryWrapper.like(QingPurIn::getRkBillNo,qingPurInRequest.getRkBillNo());
        }
        if (qingPurInRequest.getOrderTime() != null){
            Date[] orderDate = qingPurInRequest.getOrderTime();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(QingPurIn::getOrderTime, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(QingPurIn::getOrderTime,DateUtils.dateToStr(orderDate[1],"yyyy-MM-dd")+ " 23:59:59");

        }
        lambdaQueryWrapper.orderByDesc(QingPurIn::getOrderTime);
        Page<QingPurIn> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return selectPage;
    }

    @Override
    public AjaxResult<Object> importOrder(String userName, Long user_id, List<Map<String, Object>> map) throws Exception {
        if(map==null || map.size()==0) return AjaxResult.failed(MessageUtils.message("没有获取到数据,请检查模版sheet页"));
        Map<String,Object> orderMap = new HashMap<>();//key:制令单号(单据编号)val:单据头
        Map<String,Object> detailMap = new HashMap<>();//key:制令单号(单据编号) val:单据体集合
        List<String> orderNoList = new ArrayList<>();//采购订单号集合
        List<String> prdNoList = new ArrayList<>();//生产订单号集合
        try {
            Map<String, Object> headRow = map.get(0);
            String djrqHd = String.valueOf(headRow.get("采购入库单")).trim();
            String cgddHd = String.valueOf(headRow.get("__EMPTY")).trim();
            String scddHd = String.valueOf(headRow.get("__EMPTY_1")).trim();
            String wlbmHd = String.valueOf(headRow.get("__EMPTY_2")).trim();
            String ssslHd = String.valueOf(headRow.get("__EMPTY_3")).trim();
            String phHd = String.valueOf(headRow.get("__EMPTY_4")).trim();
            String ckHd = String.valueOf(headRow.get("__EMPTY_5")).trim();
            String cwHd = String.valueOf(headRow.get("__EMPTY_6")).trim();
            String bzHd = String.valueOf(headRow.get("__EMPTY_7")).trim();

            if (!"单据日期".equals(djrqHd) ||
                    !"采购订单号".equals(cgddHd) ||
                    !"生产订单编号".equals(scddHd) ||
                    !"物料编码".equals(wlbmHd) ||
                    !"实收数量".equals(ssslHd) ||
                    !"仓库".equals(ckHd) ||
                    !"仓位".equals(cwHd) ||
                    !"批号".equals(phHd) ||
                    !"备注".equals(bzHd) ) {
                return AjaxResult.failed(MessageUtils.message("导入模版错误，请检查模版格式"));
            }
        } catch (Exception e) {
            // 捕获所有异常并返回错误信息
            return AjaxResult.failed(MessageUtils.message("导入模版错误，请检查模版格式"));
        }
        //前期校验excel模板数据
        Boolean aBoolean = checkQingPurIn(userName, user_id, map);
        if (!aBoolean){
            throw new RuntimeException("模板数据校验未通过,请查看传单日志查看具体信息");
        }

        //订单同步开始
        String tbbillNo = qingSyncOrderService.startQingSyncOrder(userName,user_id,"采购入库单");
        List<String> materialNoList = new ArrayList<>();
        List<String> stockNameList = new ArrayList<>();
        //先查询出物料和车间等信息
        for (Map<String,Object>  item:map){
            String orderNo = item.get("__EMPTY").toString();//采购订单号
            if ("采购订单号".equals(orderNo)){
                continue;
            }
            String materialNo = VtuzxUtil.getString(item,"__EMPTY_2");//物料编码
            if (!materialNoList.contains(materialNo)){
                materialNoList.add(materialNo);
            }

        }
        //根据物料编码查询物料名称等信息
        List<Map<String, Object>> materialList = materialService.getqingMaterialByNo(materialNoList);
        //将查询到的物料信息转成map
        Map<String, Object> materialToMap = changeMaterialToMap(materialList);
        for (Map<String,Object>  item:map){
            String orderNo = item.get("__EMPTY").toString();//采购订单号
            if ("采购订单号".equals(orderNo)){
                continue;
            }
            if(!orderNoList.contains(orderNo)){
                orderNoList.add(orderNo);
            }
            Integer OrderTime = VtuzxUtil.getInt(item,"采购入库单");//单据日期
            String MoBillNo = VtuzxUtil.getString(item,"__EMPTY_1");//生产订单号
            String materialNo = VtuzxUtil.getString(item,"__EMPTY_2");//物料编码
            Double count = VtuzxUtil.getDouble(item,"__EMPTY_3");//实收数量
            String lot= VtuzxUtil.getString(item,"__EMPTY_4");//批号
            String stock = VtuzxUtil.getString(item,"__EMPTY_5");//仓库
            String location = VtuzxUtil.getString(item,"__EMPTY_6");//仓位
            String remark = VtuzxUtil.getString(item,"__EMPTY_7");//备注

            QingPurIn qingPurIn = VtuzxUtil.getObject(orderMap,orderNo);
            List<QingPurIn> qingpurInList = new ArrayList<>();
            Long masterId = null;
            if (null == qingPurIn){
                QingPurIn operp = new QingPurIn();
                operp.setPurBillNo(orderNo);
                operp.setOrderTime(conVerDate(OrderTime));
                operp.setSyncNo(tbbillNo);//同步单号
                operp.setRemark(remark);//同步单号
                // 插入主单据并获取ID
                qingPurInDao.insertQingPurIn(operp);
                masterId = operp.getId(); // 获取生成的主键ID

                orderMap.put(orderNo, operp);
            }
            //map中添加详情数据
            List<QingPurInDetail> orderqingDetailList = VtuzxUtil.getObject(detailMap,orderNo);
            List<QingPurInDetail> detailList = new ArrayList<>();
            if (null == orderqingDetailList || orderqingDetailList.size() == 0){
                List<QingPurInDetail> orderDetails = new ArrayList<>();
                QingPurInDetail detail = new QingPurInDetail();
                // 设置关联ID
                detail.setLinkId(String.valueOf(masterId)); // 使用主单据ID关联
                detail.setPurBillNo(orderNo);
                detail.setMoBillNo(MoBillNo);
                detail.setStock(stock);
                detail.setLocation(location);
                detail.setQty(count);
                detail.setLot(lot);
                detail.setRemark(remark);
                //查询规格等信息
                Map<String,Object> materialMap = VtuzxUtil.getObject(materialToMap,materialNo);
                if (!VtuzxUtil.isEmpty(materialMap)){
                    detail.setMaterialNo(materialNo);
                    detail.setMaterialName(VtuzxUtil.getString(materialMap,"FNAME"));
                    detail.setSpecification(VtuzxUtil.getString(materialMap,"FSPECIFICATION"));
                    detail.setUnitName(VtuzxUtil.getString(materialMap,"unitName"));
                    detail.setUnitNo(VtuzxUtil.getString(materialMap,"unitNo"));
                }
                orderDetails.add(detail);
                detailMap.put(orderNo, orderDetails);
                // 批量插入明细数据
                qingPurInDao.batchInsertQingPurInDetail(orderDetails);
            } else {
                List<QingPurInDetail> orderDetails = new ArrayList<>();
                QingPurInDetail detail = new QingPurInDetail();
                detail.setLinkId(String.valueOf(qingPurIn.getId()));
                detail.setPurBillNo(orderNo);
                detail.setMoBillNo(MoBillNo);
                detail.setStock(stock);
                detail.setLocation(location);
                detail.setQty(count);
                detail.setLot(lot);
                detail.setRemark(remark);
                detail.setMaterialNo(materialNo);
                //查询规格等信息
                Map<String,Object> materialMap = VtuzxUtil.getObject(materialToMap,materialNo);
                if (!VtuzxUtil.isEmpty(materialMap)){
                    detail.setMaterialNo(materialNo);
                    detail.setMaterialName(VtuzxUtil.getString(materialMap,"FNAME"));
                    detail.setSpecification(VtuzxUtil.getString(materialMap,"FSPECIFICATION"));
                    detail.setUnitName(VtuzxUtil.getString(materialMap,"unitName"));
                    detail.setUnitNo(VtuzxUtil.getString(materialMap,"unitNo"));
                }
                orderDetails.add(detail);
                qingPurInDao.batchInsertQingPurInDetail(orderDetails);
                orderqingDetailList.add(detail);
                detailMap.put(orderNo, orderqingDetailList);
                // 批量插入明细数据

            }

        }
        //查询已经保存过的生产订单
//        List<String> savescOrderNoList =  selectQingOperpByOrderNo(prdNoList);


        List<QingSyncOrderDetail> orderDetailList = new ArrayList<>();//需要保存到传单日志详情表中的数据
        for (String orderNo: orderNoList){
            //根据订单编号查询传单日志表
            QingSyncOrderDetail qingSyncOrderDetail = new QingSyncOrderDetail();
            qingSyncOrderDetail = new QingSyncOrderDetail();
            qingSyncOrderDetail.setOrderNo(orderNo);
            qingSyncOrderDetail.setBillNo(tbbillNo);
            qingSyncOrderDetail.setCreateTime(new Date());
            qingSyncOrderDetail.setUpdateTime(new Date());

            QingPurIn qingPurIn = VtuzxUtil.getObject(orderMap,orderNo);
            List<QingPurInDetail> qingPurInDetailList = VtuzxUtil.getObject(detailMap,orderNo);

            try {
                syncKingdeeOrderService.syncQingPurIn(qingPurIn, qingPurInDetailList,user_id);
                qingSyncOrderDetail.setIsSuccess(true);
            } catch (Exception e){
                //捕获异常删除已经保存的数据
                deleteQingPurInA(qingPurIn,qingPurInDetailList);
                _logger.info(e.getMessage());
                qingSyncOrderDetail.setIsSuccess(false);
                qingSyncOrderDetail.setMessage(e.getMessage());

            }
            orderDetailList.add(qingSyncOrderDetail);
        }
        Map<String, Object> data = qingSyncOrderService.endQingSyncOrder(orderDetailList, tbbillNo);
        return AjaxResult.success(data);
    }

    @Override
    public Map<String, Object> deleteQingPurIn(String userName, Long user_id, Map<String, Object> map) throws Exception {
                String billNoList = map.get("Lid").toString();
        //根据逗号拆分成数组
        int headCount = 0;
        int detailCount =0;
        String[] split = billNoList.split(",");
        for (String idNo : split){
            QingPurIn qingPurIn = qingPurInDao.selectById(idNo);
            // 如果数据库id是数字类型，应改为：
            Integer id = Integer.parseInt(String.valueOf(qingPurIn.getId()));
            String billNo = String.valueOf(qingPurIn.getRkBillNo());
            VtuzxKingdeeCloudClient client = null;
            try {
                client = vtuzxKingdeeService.loginQKingdeeByUser(user_id);
            } catch (Exception e){
                throw new RuntimeException("金蝶账号密码错误请维护");
            }
            //查询金蝶生产订单
            List<VtuzxMap> prdcumQuery = vtuzxKingdeeService.query(client,"STK_InStock", new VtuzxArrayList<>(
                            "FBillNo"
                    ),
                    // 组合两个过滤条件（规格型号 + 物料分组）
                    String.format("FBillNo = '%s' ",
                            billNo), null, 0, 0);

            if (!VtuzxUtil.isEmpty(prdcumQuery)) {
                throw new RuntimeException(String.format("该订单已存在不能删除订单号%s", billNo));
            }
            // 删除本地数据库记录
            int deletedDetails = qingPurInDao.delete(
                    new LambdaQueryWrapper<QingPurIn>()
                            .eq(QingPurIn::getId, id)
            );
            headCount += deletedDetails;
            int deletedMain = qingPurInDetailDao.delete(
                    new LambdaQueryWrapper<QingPurInDetail>()
                            .eq(QingPurInDetail::getLinkId, id)
            );
            detailCount += deletedMain;
        }
        Map<String, Object> data = new HashMap<>();
        data.put("success", true);
        data.put("message", String.format("主表删除 %d 条，明细表删除 %d 条", headCount, detailCount));
        return data;
    }

    /**
     * 查询已经保存过的生产订单
     * @param prdNoList
     * @return
     */
    private List<String> selectQingOperpByOrderNo(List<String> prdNoList) {
        // 创建查询条件：查询明细表中生产订单号在给定列表中的记录
        LambdaQueryWrapper<QingPurInDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(QingPurInDetail::getMoBillNo, prdNoList)
                .select(QingPurInDetail::getMoBillNo); // 只查询生产订单号字段
        // 执行查询
        List<QingPurInDetail> detailList = qingPurInDetailDao.selectList(queryWrapper);
        List<String> saveOrderNoList = new ArrayList<>();
        // 遍历查询结果，提取生产订单号并去重
        if (!VtuzxUtil.isEmpty(detailList)) {
            for (QingPurInDetail qingPurInDetail : detailList) {
                String billNo = qingPurInDetail.getMoBillNo();
                if (billNo != null && !saveOrderNoList.contains(billNo)) {
                    saveOrderNoList.add(billNo);
                }
            }
        }
        return saveOrderNoList;
    }
    /**
     * Excel日期转换
     * @param times
     * @return
     */
    public Date conVerDate(int times){
        Calendar calendar = Calendar.getInstance();
        calendar.set(1900,Calendar.JANUARY,1);
        calendar.add(Calendar.DAY_OF_MONTH,times -2);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * key;物料编码 val:物料相关信息(map)
     * @param materialList
     * @return
     */
    private Map<String,Object> changeMaterialToMap(List<Map<String,Object>> materialList){
        Map<String,Object> dataMap = new HashMap<>();
        for (Map<String,Object> data : materialList){
            String FNUMBER = VtuzxUtil.getString(data,"FNUMBER");
            String FNAME = VtuzxUtil.getString(data,"FNAME");
            String FSPECIFICATION = VtuzxUtil.getString(data,"FSPECIFICATION");
            String unitNo = VtuzxUtil.getString(data,"unitNo");//单位编码
            String unitName = VtuzxUtil.getString(data,"unitName");//单位名称
            String FDOCUMENTSTATUS = VtuzxUtil.getString(data,"FDOCUMENTSTATUS");//审核状态
            String FFORBIDSTATUS = VtuzxUtil.getString(data,"FFORBIDSTATUS");//禁用状态
            Map<String,Object> materialMap = new HashMap<>();
            materialMap.put("FNUMBER",FNUMBER);
            materialMap.put("FNAME",FNAME);
            materialMap.put("FSPECIFICATION",FSPECIFICATION);
            materialMap.put("unitNo",unitNo);
            materialMap.put("unitName",unitName);
            materialMap.put("FDOCUMENTSTATUS",FDOCUMENTSTATUS);
            materialMap.put("FFORBIDSTATUS",FFORBIDSTATUS);
            dataMap.put(FNUMBER,materialMap);
        }
        return dataMap;
    }
    /**
     * 校验销售订单导入的数据
     * @param user_id
     * @param map
     * @return
     * @throws Exception
     */
    private Boolean checkQingPurIn(String userName,Long user_id,List<Map<String,Object>> map) throws Exception{
        //添加行号
        int i = 3;
        for (Map<String,Object> data : map){
            data.put("line",i);
            i++;
        }
        //添加之前做一个删除
        materialService.deleteQingpurInOrder(userName,user_id);
        //sqlserver数据库添加当前导入的数据
        materialService.saveQingPurIn(userName,user_id,map);
        //校验指令单号
        List<Map<String,Object>> checkQingMoNo =  materialService.checkQingpurInBillNo(user_id);
        //校验物料
        List<Map<String,Object>> checkQingMaterial =  materialService.checkQingpurInMaterial(user_id);
        //校验数量
        List<Map<String,Object>> checkQingInstockCount =  materialService.checkQingpurInCount(user_id);
        //校验时间
        List<Map<String,Object>> checkQingpurorderTime =  materialService.checkQingpurorderTime(user_id);
        //校验时间
        List<Map<String,Object>> checkQingMoBillNo =  materialService.checkQingMoBillNo(user_id);
        //校验仓库
        List<Map<String,Object>> checkQingStock =  materialService.checkQingpurInStock(user_id);
        //校验仓位
        List<Map<String,Object>> checkQingLocation =  materialService.checkQingpurisLocation(user_id);


        //判断查询的数据如果都为空的话说明模板数据校验通过可以导入,如果不为空的话添加日志信息并且停止传单
        if (VtuzxUtil.isEmpty(checkQingMoNo) &&  VtuzxUtil.isEmpty(checkQingMaterial) && VtuzxUtil.isEmpty(checkQingInstockCount)&& VtuzxUtil.isEmpty(checkQingStock) && VtuzxUtil.isEmpty(checkQingpurorderTime) && VtuzxUtil.isEmpty(checkQingLocation) && VtuzxUtil.isEmpty(checkQingMoBillNo)){
            return true;
        }  else {//有一个不等于空的就说明有问题,需要向日志中添加信息
            saveQingPurInLog(userName,user_id,checkQingMoNo,checkQingMaterial,checkQingInstockCount,checkQingStock,checkQingpurorderTime,checkQingLocation,checkQingMoBillNo);
            return false;
        }

    }


    @Override
    public Map<String, Object> getQingPurInDetail(Map<String, Object> map) {
        // 如果数据库id是数字类型，应改为：
        Integer billNo = Integer.parseInt(map.get("billNo").toString());
        QingPurIn qingpurinform = qingPurInDao.selectOne(new LambdaQueryWrapper<QingPurIn>()
                .eq(QingPurIn::getId,billNo));
        List<QingPurInDetail> qingpurinList = qingPurInDetailDao.selectList(new LambdaQueryWrapper<QingPurInDetail>()
                .eq(QingPurInDetail::getLinkId,billNo));
        Map<String,Object> data = new HashMap<>();
        data.put("data",qingpurinform);
        data.put("dataList",qingpurinList);
        return data;
    }


    //删除校验
    private void deleteQingPurInA (QingPurIn qingPurIn, List<QingPurInDetail> qingPurInDetailList){
        List<Long> detailIdList = new ArrayList<>();

        for (QingPurInDetail detail : qingPurInDetailList){
            Long id = detail.getId();
            if (!detailIdList.contains(id)){
                detailIdList.add(id);
            }
        }
        qingPurInService.removeById(qingPurIn.getId());
        qingPurInDetailService.removeBatchByIds(detailIdList);
    }


    /**
     * 模板级校验添加日志信息
     * @param checkQingMoNo
     * @param checkQingMaterial

     */
    private void saveQingPurInLog(String userName,Long user_id,List<Map<String,Object>> checkQingMoNo,List<Map<String,Object>> checkQingMaterial,List<Map<String,Object>> checkQingInstockCount,
                              List<Map<String,Object>> checkQingStock,List<Map<String,Object>> checkQingpurorderTime,List<Map<String,Object>> checkQingLocation  ,List<Map<String,Object>>   checkQingMoBillNo){
        String tbbillNo = qingSyncOrderService.startQingSyncOrder(userName,user_id,"采购库单模板校验");
        List<QingSyncOrderDetail> orderDetailList = new ArrayList<>();
        if (!VtuzxUtil.isEmpty(checkQingMoNo)){//指令单号校验
            for (Map<String,Object> data : checkQingMoNo){
                int line = VtuzxUtil.getInt(data,"LINE");//行号
                QingSyncOrderDetail qingSyncOrderDetail = new QingSyncOrderDetail();
                qingSyncOrderDetail.setOrderNo("采购库单模板校验");//客户订单号
                qingSyncOrderDetail.setBillNo(tbbillNo);
                qingSyncOrderDetail.setCreateTime(new Date());
                qingSyncOrderDetail.setUpdateTime(new Date());
                qingSyncOrderDetail.setIsSuccess(false);
                qingSyncOrderDetail.setMessage("第" + line + "行的采购订单号不允许为空、采购订单未生成、采购订单未审核");
                orderDetailList.add(qingSyncOrderDetail);
            }
        }

        if (!VtuzxUtil.isEmpty(checkQingMaterial)){//物料校验
            for (Map<String,Object> data : checkQingMaterial){
                int line = VtuzxUtil.getInt(data,"LINE");//行号
                QingSyncOrderDetail qingSyncOrderDetail = new QingSyncOrderDetail();
                qingSyncOrderDetail.setOrderNo("采购库单模板校验");//客户订单号
                qingSyncOrderDetail.setBillNo(tbbillNo);
                qingSyncOrderDetail.setCreateTime(new Date());
                qingSyncOrderDetail.setUpdateTime(new Date());
                qingSyncOrderDetail.setIsSuccess(false);
                qingSyncOrderDetail.setMessage("第" + line + "行的物料编码未审核,或者不存在,或者已禁用");
                orderDetailList.add(qingSyncOrderDetail);
            }
        }

        if (!VtuzxUtil.isEmpty(checkQingInstockCount)){//校验数量
            for (Map<String,Object> data : checkQingInstockCount){
                int line = VtuzxUtil.getInt(data,"LINE");//行号
                QingSyncOrderDetail qingSyncOrderDetail = new QingSyncOrderDetail();
                qingSyncOrderDetail.setOrderNo("采购库单模板校验");//客户订单号
                qingSyncOrderDetail.setBillNo(tbbillNo);
                qingSyncOrderDetail.setCreateTime(new Date());
                qingSyncOrderDetail.setUpdateTime(new Date());
                qingSyncOrderDetail.setIsSuccess(false);
                qingSyncOrderDetail.setMessage("第" + line + "行的数量不能小于等于0");
                orderDetailList.add(qingSyncOrderDetail);
            }
        }



        if (!VtuzxUtil.isEmpty(checkQingStock)){//开工时间
            for (Map<String,Object> data : checkQingStock){
                int line = VtuzxUtil.getInt(data,"LINE");//行号
                QingSyncOrderDetail qingSyncOrderDetail = new QingSyncOrderDetail();
                qingSyncOrderDetail.setOrderNo("采购库单模板校验");//客户订单号
                qingSyncOrderDetail.setBillNo(tbbillNo);
                qingSyncOrderDetail.setCreateTime(new Date());
                qingSyncOrderDetail.setUpdateTime(new Date());
                qingSyncOrderDetail.setIsSuccess(false);
                qingSyncOrderDetail.setMessage("第" + line + "行仓库不能为空,仓库未审核,仓库被禁用，仓库不存在");
                orderDetailList.add(qingSyncOrderDetail);
            }
        }

        if (!VtuzxUtil.isEmpty(checkQingpurorderTime)){//开工时间
            for (Map<String,Object> data : checkQingpurorderTime){
                int line = VtuzxUtil.getInt(data,"LINE");//行号
                QingSyncOrderDetail qingSyncOrderDetail = new QingSyncOrderDetail();
                qingSyncOrderDetail.setOrderNo("采购库单模板校验");//客户订单号
                qingSyncOrderDetail.setBillNo(tbbillNo);
                qingSyncOrderDetail.setCreateTime(new Date());
                qingSyncOrderDetail.setUpdateTime(new Date());
                qingSyncOrderDetail.setIsSuccess(false);
                qingSyncOrderDetail.setMessage("第" + line + "行入库时间不能为空、不能小于采购订单时间");
                orderDetailList.add(qingSyncOrderDetail);
            }
        }

        if (!VtuzxUtil.isEmpty(checkQingLocation)){//完工时间
            for (Map<String,Object> data : checkQingLocation){
                int line = VtuzxUtil.getInt(data,"LINE");//行号
                QingSyncOrderDetail qingSyncOrderDetail = new QingSyncOrderDetail();
                qingSyncOrderDetail.setOrderNo("采购库单模板校验");//客户订单号
                qingSyncOrderDetail.setBillNo(tbbillNo);
                qingSyncOrderDetail.setCreateTime(new Date());
                qingSyncOrderDetail.setUpdateTime(new Date());
                qingSyncOrderDetail.setIsSuccess(false);
                qingSyncOrderDetail.setMessage("第" + line + "行的仓库未启用仓位、仓库启用仓位但未填写仓位值");
                orderDetailList.add(qingSyncOrderDetail);
            }
        }
        if (!VtuzxUtil.isEmpty(checkQingMoBillNo)){//完工时间
            for (Map<String,Object> data : checkQingMoBillNo){
                int line = VtuzxUtil.getInt(data,"LINE");//行号
                QingSyncOrderDetail qingSyncOrderDetail = new QingSyncOrderDetail();
                qingSyncOrderDetail.setOrderNo("采购库单模板校验");//客户订单号
                qingSyncOrderDetail.setBillNo(tbbillNo);
                qingSyncOrderDetail.setCreateTime(new Date());
                qingSyncOrderDetail.setUpdateTime(new Date());
                qingSyncOrderDetail.setIsSuccess(false);
                qingSyncOrderDetail.setMessage("第" + line + "生产订单编号重复，请检查当前表格和已经导入的单据");
                orderDetailList.add(qingSyncOrderDetail);
            }
        }

        qingSyncOrderService.endQingSyncOrder(orderDetailList,tbbillNo);
    }






}
